package cn.bobohost.crmrbac.util;

import cn.bobohost.crmrbac.pojo.po.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author jy
 * @version 1.0
 * @date 2022/2/10 21:41
 */
public interface UserContext {
    String USER_IN_SESSION = "USER_IN_SESSION";
    String PERMISSION_IN_SESSION = "PERMISSION_IN_SESSION";

    static HttpSession getSession() {

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        //设置session的生命时间永不过期(除了注销)
//        requestAttributes.getRequest().getSession().setMaxInactiveInterval(-1);
        return requestAttributes.getRequest().getSession();
    }


    static void saveEmployee(Employee employee) {

        getSession().setAttribute(USER_IN_SESSION,employee);
    }

    static Employee getEmployee(){
        return (Employee) getSession().getAttribute(USER_IN_SESSION);
    }

    static void savePermission(List<String> permissionsStr) {
        getSession().setAttribute(PERMISSION_IN_SESSION,permissionsStr);
    }
    static List<String> getPermission() {
        return (List<String>) getSession().getAttribute(PERMISSION_IN_SESSION);
    }

}
